package com.nbcio.online.controller;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nbcio.common.core.domain.R;
import com.nbcio.common.mybatis.core.page.PageQuery;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.common.mybatis.helper.DataBaseHelper;
import com.nbcio.common.web.core.BaseController;
import com.nbcio.generator.domain.GenTable;
import com.nbcio.online.service.IFormService;

import lombok.RequiredArgsConstructor;

/**
 * online表单功能
 *
 * @author nbacheng
 */

@RequiredArgsConstructor
@RestController
@RequestMapping("/online")
public class FormControl extends BaseController {
	
	private final IFormService formService;
	
	/**
     * 查询online列表
     */
    @GetMapping("/list")
    public TableDataInfo<GenTable> onlineList(GenTable genTable, PageQuery pageQuery) {
        return formService.selectPageGenTableList(genTable, pageQuery);
    }
	
	/**
     * 查询online列表
     *
     * @param tableId 表tableId
     */
    @GetMapping("/{tableId}")
    public R<?> getOnlineForm(@PathVariable("tableId")  Long tableId) {
    	return R.ok(formService.getOnlineForm(tableId));
    }
    
    /**
     * 查询数据源名称列表
     */
    @GetMapping(value = "/getDataNames")
    public R<Object> getCurrentDataSourceNameList(){
        return R.ok(DataBaseHelper.getDataSourceNameList());
    }
    
    /**
     * 查询online表数据
     *
     * @param tableId 表id, tableName 表名
     */
    @GetMapping("/getData/{tableId}/{tableName}")
    public TableDataInfo<Map> getOnlineData(@PathVariable("tableId")  Long tableId, 
    		          @PathVariable("tableName")  String tableName, PageQuery pageQuery) {
    	return formService.listOnlineData(tableId, tableName, pageQuery);
    }
    
    /**
     * 更新online表数据
     *
     * @param tableId 表id, tableName 表名
     */
    @PostMapping("/addOrUpdateData/{tableId}/{tableName}")
    public R<Object> addOrUpdateOnlineData(@PathVariable("tableId")  Long tableId, 
    		          @PathVariable("tableName")  String tableName, @RequestBody Map<String, Object> map) {
    	return R.ok(formService.addOrUpdateOnlineData(tableId, tableName, map));
    }
    
    /**
     * 删除online表数据
     *
     * @param tableId 表id, tableName 表名 , ids 删除的id
     */
    @DeleteMapping("/delOnlineData/{tableId}/{tableName}")
    public R<Void> delOnlineData(
        @PathVariable("tableId") Long tableId,
        @PathVariable("tableName") String tableName,
        @RequestBody List<Long> ids) {
        
        // 表名校验（示例校验）
        if (!tableName.matches("^[a-zA-Z_][a-zA-Z0-9_]*$")) {
            throw new IllegalArgumentException("非法表名");
        }
        
        formService.deleteOnlineData(tableId, tableName, ids);
        return R.ok("删除成功！");
    }
    
    /**
     * 查询online的id详细数据
     *
     * @param tableId 表id, tableName 表名, id 查询的id
     */
    @PostMapping("/getOnlineData/{tableId}/{tableName}")
    public R<Object> getOnlineData(@PathVariable("tableId")  Long tableId, 
    		          @PathVariable("tableName")  String tableName, @RequestBody Long id) {
    	return R.ok(formService.getOnlineData(tableId, tableName, id));
    }
 
}
